package com.example.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.example.result.Result;
import com.example.service.IndexService;


/**
 * @author 袁小弘
 * @describe 登录和首页信息查询
 * @date 2019-12-31
 */
@RestController
@RequestMapping("/api")
public class IndexController {

    @Autowired
    IndexService indexService;

    /**
     * @api {post}/api/login
     * @apiName 登录接口
     * @apiGroup Index
     * @apiParam {String} json
     * @apiParam {HttpSession} session
     *
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody String json, HttpSession session) {
        System.out.println("jsons数据：" + json);

//		 JSONObject parseObject = JSON.parseObject(json);
        Map<String, Object> map = (Map<String, Object>) JSON.parseObject(json);

        Result list = indexService.login(map);

        if (null == list) {
            return new Result(400);
        } else {
            session.setAttribute("user", list);
            return list;
        }

    }


    @ApiOperation(value="查询用户", notes="根据UserId查询用户对象")
    @ApiImplicitParam(name = "userId", value = "用户详细实体user", required = true, dataType = "String")
    @RequestMapping(value = "/userInfo/{userId}", method = RequestMethod.GET)
    public Result userInfoById(@PathVariable("userId") String userId) {
        return indexService.userInfoById(userId);
    }

    /**
     * @param userId
     * @return 查询成功返回数据，查询失败返回一个查询失败提示信息
     * @describe 根据编号查询学生用户的课程信息
     */
    @RequestMapping(value = "/courseInfo/{userId}", method = RequestMethod.GET)
    public Result courseInfoById(@PathVariable("userId") String userId) {
        return indexService.courseInfoById(userId);
    }

    /**
     * @param userId
     * @return 查询成功返回数据，查询失败返回一个查询失败提示信息
     * @describe 根据编号查询老师用户的课程信息
     */
    @RequestMapping(value = "/courseTeacherInfo/{userId}", method = RequestMethod.GET)
    public Result courseInfoByTeacherId(@PathVariable("userId") String userId) {
        return indexService.courseInfoByTeacherId(userId);
    }

    /**
     * @return 查询成功返回数据，查询失败返回一个查询失败提示信息
     * @describe 计算所有用户数量
     */
    @RequestMapping(value = "/calculateUser", method = RequestMethod.GET)
    public Result calculateAllUsers() {
        return indexService.calculateAllUsers();
    }

    /**
     * @return 查询成功返回数据，查询失败返回一个查询失败提示信息
     * @describe 计算同一角色的用户数量
     */
    @RequestMapping(value = "/calculateUserByRole", method = RequestMethod.GET)
    public Result calculateAllUsersByRoleName() {
        return indexService.calculateAllUsersByRoleName();
    }

    /**
     * @return 查询成功返回数据，查询失败返回一个查询失败提示信息
     * @describe 统计请假未处理数量
     */
    @RequestMapping(value = "/calculateVecate", method = RequestMethod.GET)
    public Result calculateVecateNumber() {
        return indexService.calculateVecateNumber();
    }

    /**
     * @return 查询成功返回数据，查询失败返回一个查询失败提示信息
     * @describe 统计转班未处理数量
     */
    @RequestMapping(value = "/calculateTransfer", method = RequestMethod.GET)
    public Result calculateTransferNumber() {
        return indexService.calculateTransferNumber();
    }

    /**
     * @return 查询成功返回数据，查询失败返回一个查询失败提示信息
     * @describe 统计退班未处理数量
     */
    @RequestMapping(value = "/calculateQuit", method = RequestMethod.GET)
    public Result calculateQuitNumber() {
        return indexService.calculateQuitNumber();
    }

    /**
     * @return 查询成功返回数据，查询失败返回一个查询失败提示信息
     * @describe 根据角色查询权限
     */
    @RequestMapping(value = "/roleMenuResult/{roleName}", method = RequestMethod.GET)
    public Result roleMenuResult(@PathVariable("roleName") String roleName) {
        return indexService.roleMenuResult(roleName);
    }
}
